package edu.ucsd.cse110;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import edu.ucsd.cse110.model.Lift;

/**
 * TODO: Need documentation.
 */
public class StatsLiftGraph {
	private int liftId;

	/**
	 * TODO: Need documentation.
	 * 
	 * @param lift
	 * @param id
	 */
	public StatsLiftGraph(String lift, int id) {
		liftId = id;
	}

	/**
	 * TODO: Need documentation.
	 * 
	 * @param context
	 * @return
	 */
	public Intent getIntent(Context context) {
		Format formatter = new SimpleDateFormat("dd-MMM-yy");
		LocalDatabaseManager database = new LocalDatabaseManager(context);
		List<Lift> lift = database.getLiftByLiftId(liftId);
		database.close();

		if (lift.size() > 1) {
			TimeSeries series1 = new TimeSeries("One Rep");

			// x must equal y in length
			for (int i = 1; i < lift.size(); i++) {
				series1.add(i, lift.get(i).getRepOne());
			}

			TimeSeries series2 = new TimeSeries("Three Reps");
			// x must equal y in length
			for (int i = 1; i < lift.size(); i++) {
				series2.add(i, lift.get(i).getRepThree());
			}

			TimeSeries series3 = new TimeSeries("Five Reps");
			// x must equal y in length
			for (int i = 1; i < lift.size(); i++) {
				series3.add(i, lift.get(i).getRepFive());
			}

			XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
			dataset.addSeries(series1);
			dataset.addSeries(series2);
			dataset.addSeries(series3);

			// customization for series1
			XYSeriesRenderer renderer = new XYSeriesRenderer();
			renderer.setColor(Color.WHITE);
			renderer.setPointStyle(PointStyle.CIRCLE);
			renderer.setFillPoints(true);
			renderer.setChartValuesTextSize(25);
			renderer.setDisplayChartValues(true);

			// customization for series2
			XYSeriesRenderer renderer2 = new XYSeriesRenderer();
			renderer2.setColor(Color.CYAN);
			renderer2.setPointStyle(PointStyle.DIAMOND);
			renderer2.setFillPoints(true);
			renderer2.setChartValuesTextSize(25);
			renderer2.setGradientEnabled(true);
			renderer2.setDisplayChartValues(true);

			// customization for series3
			XYSeriesRenderer renderer3 = new XYSeriesRenderer();
			renderer3.setColor(Color.RED);
			renderer3.setPointStyle(PointStyle.SQUARE);
			renderer3.setFillPoints(true);
			renderer3.setChartValuesTextSize(25);
			renderer3.setDisplayChartValues(true);

			XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
			mRenderer.addSeriesRenderer(renderer);
			mRenderer.addSeriesRenderer(renderer2);
			mRenderer.addSeriesRenderer(renderer3);
			// mRenderer.setChartTitle("" + lift + " Graph");
			mRenderer.setChartTitleTextSize(30);
			mRenderer.setBackgroundColor(Color.WHITE);
			mRenderer.setShowGrid(true);
			mRenderer.setGridColor(Color.GRAY);
			mRenderer.setXTitle("Date");
			mRenderer.setYTitle("Weight Lifted (lbs)");
			mRenderer.setZoomButtonsVisible(true);
			mRenderer.setXLabels(0);

			// Enters data into the graph
			for (int i = 1; i < lift.size(); i++) {
				mRenderer.addXTextLabel(i,
						formatter.format(lift.get(i).getDateOne().getTime()));
			}
			mRenderer.setBarSpacing(20);
			Intent intent = ChartFactory.getLineChartIntent(context, dataset,
					mRenderer, "Lift Progression");

			return intent;
		}
		return null;
	}
}